﻿// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证（版本 2.0）进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动！任何基于本项目二次开发而产生的一切法律纠纷和责任，我们不承担任何责任！

using Admin.NET.Core.Service;
using Microsoft.AspNetCore.Http;
using Pano.MC.Application.Entity;

namespace Pano.MC.Application;

/// <summary>
/// 项目服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Name = "mcProject", Order = 100)]
public class McProjectService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<McProject> _mcProjectRep;

    public McProjectService(SqlSugarRepository<McProject> mcProjectRep)
    {
        _mcProjectRep = mcProjectRep;
    }

    /// <summary>
    /// 分页查询项目
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>    
    [ApiDescriptionSettings(Name = "page", Description = "分页查询", Order = 1000), HttpPost]
    [DisplayName("分页查询项目")]
    public async Task<SqlSugarPagedList<McProjectOutput>> Page(PageMcProjectInput input)
    {
        var query = _mcProjectRep.AsQueryable()
            .Select<McProjectOutput>();
		return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加项目
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "add", Description = "增加项目", Order = 990), HttpPost]
    [DisplayName("增加项目")]
    public async Task<long> Add(AddMcProjectInput input)
    {
        var entity = input.Adapt<McProject>();
        await _mcProjectRep.InsertAsync(entity);
        return entity.Id;
    }

    /// <summary>
    /// 删除项目
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "delete", Description = "删除(项目)", Order = 980), HttpPost]
    [DisplayName("删除项目")]
    public async Task Delete(DeleteMcProjectInput input)
    {
        var entity = await _mcProjectRep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _mcProjectRep.FakeDeleteAsync(entity); // 假删除
        //await _mcProjectRep.DeleteAsync(entity); // 真删除
    }

    /// <summary>
    /// 更新项目
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "update", Description = "更新项目", Order = 970), HttpPost]
    [DisplayName("更新项目")]
    public async Task Update(UpdateMcProjectInput input)
    {
        var entity = input.Adapt<McProject>();
        await _mcProjectRep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取项目
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "detail", Description = "获取项目", Order = 960), HttpGet]
    [DisplayName("获取项目")]
    public async Task<McProject> Detail([FromQuery] QueryByIdMcProjectInput input)
    {
        return await _mcProjectRep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取项目列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "list", Description = "获取项目列表", Order = 950), HttpGet]
    [DisplayName("获取项目列表")]
    public async Task<List<McProjectOutput>> List([FromQuery] PageMcProjectInput input)
    {
        return await _mcProjectRep.AsQueryable().Select<McProjectOutput>().ToListAsync();
    }





}

